package org.ecshop.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.ecshop.models.Category;
import org.ecshop.models.Goods;

import will.icecoffe.core.DataAccessException;
import will.icecoffe.core.SimpleDataAccessService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class CategoryController implements ModelDriven<Object> {

	private List<Integer> ids = new ArrayList<Integer>();

	public void setIds(List<Integer> ids) {
		this.ids = ids;
	}
	
	@Resource(name="DAS")
	private SimpleDataAccessService<Category> das;
	@Resource(name="DAS")
	private SimpleDataAccessService<Goods> gdas;
	
	
	private Map<Integer, String> catsMap = 
			(Map<Integer, String>) ActionContext.getContext().getApplication()
			.get("catsmap");
	
	public String index() throws DataAccessException {
		if(catsMap == null || catsMap.size() ==0) {
			try {
				update();
			} catch (DataAccessException e) {
				e.printStackTrace();
			}
		}
		Map<String, int[]> tempmap = new HashMap<String, int[]>();
		//tempmap.put("gids", ids.);
		List<Goods> goodsList = gdas.find("hql", "findGoodsByIds", tempmap);
		
		System.out.println(goodsList.size());
		
		return null;
	}
	
	public String update() throws DataAccessException {
		
		//Map<Integer, String> temp = new HashMap<Integer, String>();
		List<Category> catList = das.find("hql", "findAllCategory");
		catsMap = new HashMap<Integer, String>();
		
		for(Category c : catList){
			catsMap.put(c.getCatId().intValue(), c.getCatName());
		}
		
		ActionContext.getContext().getApplication().put("catsmap", catsMap);
		
		return null;
	}

	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return null;
	}

}
